package com.comtime.usercenter;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.InputType;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.comtime.service.MyService;
import com.comtime.smartbox.BaseActivity;
import com.comtime.smartbox.HBApplication;
import com.comtime.smartkilimanjaro.R;

public class ChangeAccountManagerPwdActivity extends BaseActivity implements
		OnClickListener {
	EditText edt_oldpwd, edt_newpwd, edt_pwd_two;
	Button btn_sure;

	ImageView image_show_oldpwd, image_show_newpwd, image_show_two_pwd;
	ImageView image_clear_oldpwd, image_clear_newpwd, image_clear_two_pwd;

	HBApplication application;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_user_changeaccount_pwd);
		application=(HBApplication) getApplication();
		 init();
		 
		 regist();
		 handler.post(clearRunnable);
	}

	public void BackAction(View v) {
		finish();
		overridePendingTransition(R.anim.enter_left, R.anim.exit_right);
	}

	void init() {
		edt_oldpwd = (EditText) findViewById(R.id.et_oldpwd);
		edt_newpwd = (EditText) findViewById(R.id.et_newpwd);
		edt_pwd_two = (EditText) findViewById(R.id.et_again_pwd);

		image_show_oldpwd = (ImageView) findViewById(R.id.image_oldpwd_show);
		image_show_newpwd = (ImageView) findViewById(R.id.image_newpwd_show);
		image_show_two_pwd = (ImageView) findViewById(R.id.image_again_pwd_show);

		image_show_newpwd.setOnClickListener(this);
		image_show_oldpwd.setOnClickListener(this);
		image_show_two_pwd.setOnClickListener(this);

		image_clear_oldpwd = (ImageView) findViewById(R.id.image_oldpwd_delete);
		image_clear_newpwd = (ImageView) findViewById(R.id.image_newpwd_delete);
		image_clear_two_pwd = (ImageView) findViewById(R.id.image_again_pwd_delete);

		image_clear_oldpwd.setOnClickListener(this);
		image_clear_newpwd.setOnClickListener(this);
		image_clear_two_pwd.setOnClickListener(this);

		btn_sure = (Button) findViewById(R.id.bt_sure);
		btn_sure.setOnClickListener(this);
	}

	void regist() {
		IntentFilter filter= new IntentFilter(MyService.ACTION_DISCONNECTED);
		filter.addAction(MyService.ACTION_ADMINEPWD_UPDATE_RESULT);
		registerReceiver(receiver,filter);
	}
	String action="";
	BroadcastReceiver receiver=new BroadcastReceiver() {
		
		@Override
		public void onReceive(Context arg0, Intent intent) {
			action = intent.getAction();
			if (action.equals(MyService.ACTION_DISCONNECTED)) {
				handler.sendEmptyMessage(1);
			} else if (action.equals(MyService.ACTION_ADMINEPWD_UPDATE_RESULT)) {
				int add_result_num = intent
						.getIntExtra(MyService.VALUE_INFO, 0);
				Message message = new Message();
				message.what = 2;
				message.obj = add_result_num;
				handler.sendMessage(message);

			}
			
		}
	};
	Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {

			switch (msg.what) {
			case 1:
				showToast(getText(R.string.device_dis_connect).toString());
				ChangeAccountManagerPwdActivity.this.finish();
				overridePendingTransition(R.anim.enter_left, R.anim.exit_right);
				break;
			case 2:
				// 添加用户结果
				int add_result_num = (Integer) msg.obj;
				if (add_result_num == 0) {
					showToast(getString(R.string.pwd_manager_change_fail).toString());
				} else {
					showToast(getString(R.string.pwd_manager_change_success).toString());
					handler.postDelayed(new Runnable() {

						@Override
						public void run() {
							ChangeAccountManagerPwdActivity.this.finish();
							overridePendingTransition(R.anim.enter_left,
									R.anim.exit_right);
						}
					}, 1000);
				}
				break;

			default:
				break;
			}
		};
	};

	/**
	 * 显示或隐藏删除图片
	 * 
	 * @param str
	 */

	Runnable clearRunnable = new Runnable() {

		@Override
		public void run() {

			boolean oldpwd_flag = TextUtils.isEmpty(edt_oldpwd.getText());
			boolean newpwd_flag = TextUtils.isEmpty(edt_newpwd.getText());
			boolean newpwd_two_flag = TextUtils.isEmpty(edt_pwd_two.getText());
			if (oldpwd_flag) {
				image_clear_oldpwd.setVisibility(View.INVISIBLE);
			} else {
				image_clear_oldpwd.setVisibility(View.VISIBLE);
			}
			if (newpwd_flag) {
				image_clear_newpwd.setVisibility(View.INVISIBLE);
			} else {
				image_clear_newpwd.setVisibility(View.VISIBLE);
			}

			if (newpwd_two_flag) {
				image_clear_two_pwd.setVisibility(View.INVISIBLE);
			} else {
				image_clear_two_pwd.setVisibility(View.VISIBLE);
			}

			if (!oldpwd_flag && !newpwd_flag && !newpwd_two_flag) {
				btn_sure.setEnabled(true);
				btn_sure.setTextColor(getResources().getColor(
						R.color.item_white));
				btn_sure.setBackgroundResource(R.drawable.bg_round_stroke_white_5dp);
			} else {
				btn_sure.setEnabled(false);
				btn_sure.setTextColor(getResources()
						.getColor(R.color.item_gray));
				btn_sure.setBackgroundResource(R.drawable.bg_round_stroke_gray_5dp);
			}

			handler.postDelayed(clearRunnable, 500);
		}
	};

	boolean show_oldpwd_flag = false;// 默认隐藏
	boolean show_newpwd_flag = false;// 默认隐藏
	boolean show_again_pwd_flag = false;// 默认隐藏

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.image_oldpwd_delete:
			edt_oldpwd.setText("");
			break;
		case R.id.image_oldpwd_show:
			// 显示密码

			show_oldpwd_flag = !show_oldpwd_flag;
			if (show_oldpwd_flag) {
				// 显示密码
				edt_oldpwd
						.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
				image_show_oldpwd.setImageResource(R.drawable.icon_show);
			} else {
				// 隐藏密码
				edt_oldpwd.setInputType(InputType.TYPE_CLASS_TEXT
						| InputType.TYPE_TEXT_VARIATION_PASSWORD);
				image_show_oldpwd.setImageResource(R.drawable.icon_hide);
			}
			break;

		case R.id.image_newpwd_delete:
			edt_newpwd.setText("");
			break;
		case R.id.image_newpwd_show:
			// 显示密码

			show_newpwd_flag = !show_newpwd_flag;
			if (show_newpwd_flag) {
				// 显示密码
				edt_newpwd
						.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
				image_show_newpwd.setImageResource(R.drawable.icon_show);
			} else {
				// 隐藏密码
				edt_newpwd.setInputType(InputType.TYPE_CLASS_TEXT
						| InputType.TYPE_TEXT_VARIATION_PASSWORD);
				image_show_newpwd.setImageResource(R.drawable.icon_hide);
			}
			break;
		case R.id.image_again_pwd_delete:
			edt_pwd_two.setText("");
			break;
		case R.id.image_again_pwd_show:
			// 显示密码

			show_again_pwd_flag = !show_again_pwd_flag;
			if (show_again_pwd_flag) {
				// 显示密码
				edt_pwd_two
						.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
				image_show_two_pwd.setImageResource(R.drawable.icon_show);
			} else {
				// 隐藏密码
				edt_pwd_two.setInputType(InputType.TYPE_CLASS_TEXT
						| InputType.TYPE_TEXT_VARIATION_PASSWORD);
				image_show_two_pwd.setImageResource(R.drawable.icon_hide);
			}
			break;
		case R.id.bt_sure:
			sure();
			break;

		default:
			break;
		}

	}

	/**
	 * 确定
	 */
	void sure(){
		
		String old_phonenum=edt_oldpwd.getText().toString();
		String new_phonenum=edt_newpwd.getText().toString();
		String new_phonenum_two=edt_pwd_two.getText().toString();
		if(old_phonenum.length()!=6){
			showToast(getText(R.string.pwd_old_length_wrong).toString());
			return;
		}
		if(new_phonenum.length()!=6){
			showToast(getText(R.string.pwd_new_length_wrong).toString());
			return;
		}
		if(new_phonenum_two.length()!=6){
			showToast(getText(R.string.pwd_new_again_length_wrong).toString());
			return;
		}
		if(!new_phonenum.equals(new_phonenum_two)){
			showToast(getText(R.string.pwd_new_and_again_wrong).toString());
			return;
		}
		
		application.swDevicesList.get(0).sendChangeAdministratorPwd(old_phonenum, new_phonenum);
		
	}
	/**
	 * 自定义提示
	 * 
	 * @param str
	 */
	Toast toast = null;

	void showToast(String str) {
		if (toast != null) {
			toast.cancel();
		}
		LayoutInflater inflater = getLayoutInflater();
		View view = inflater.inflate(R.layout.toastshow, null);
		TextView textView = (TextView) view.findViewById(R.id.toastshow);
		textView.setText(str);
		toast = new Toast(this);
		double d = getResources().getDisplayMetrics().density;
		toast.setGravity(Gravity.FILL_HORIZONTAL | Gravity.CENTER, 0, 0);
		toast.setDuration(Toast.LENGTH_SHORT);
		toast.setView(view);
		toast.show();
	}
	
	
	protected void onDestroy() {
		super.onDestroy();
		unregisterReceiver(receiver);

	};
}
